package com.pswv2.backstage.risk.action;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.omg.CORBA.portable.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.aliyun.oss.model.Style;
import com.pswv2.backstage.category.servic.CategoryService;
import com.pswv2.backstage.keyword.service.KWordService;
import com.pswv2.backstage.risk.servic.RiskService;
import com.pswv2.bub.bsc.dao.po.Category;
import com.pswv2.bub.bsc.dao.po.KWord;
import com.pswv2.bub.bsc.dao.po.Risk;
import com.pswv2.bub.bsc.dao.po.Role;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;
import com.pswv2.util.Sessions;
import com.pswv2.util.Strings;

@Controller
@RequestMapping(value = "riskAction", produces = { "text/json;charset=UTF-8" })
public class RiskAction {
	private static Class<?> poc = Risk.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private RiskService riskService;

	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = { "addRisk" })
	public String add(String kWord,String content,Integer userId,HttpSession session) {
		Risk risk = new Risk();
		risk.setkWord(kWord);
		risk.setContent(content);
		risk.setUserId(userId);
		riskService.add(risk);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = { "listAll" })
	public String list(HttpSession session) {
		List allcategory =  riskService.queryAll();
		return CommonResp.buildSuccessResp(allcategory).toJsonString();
	}
	
	/**
	 * 查看所有该用户的
	 */
	@ResponseBody
	@RequestMapping(value = { "listAllUser" })
	public String listAllUser(Integer userId) {
		List allRisk =  riskService.queryAllUser(userId);
		return CommonResp.buildSuccessResp(allRisk).toJsonString();
	}
	
	/**
	 * 查看所有
	 */
	 
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize, HttpSession session) {
		pageIndex=pageIndex*pageSize;
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("categoryId"),pageIndex, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	/**
	 * 通过Id查询
	 */
	
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		Risk risk = riskService.queryById(id);
		if (risk == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(risk).toJsonString();
	}

	/**
	 * 修改
	 */
		
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(Integer id, String kWord, String content,  HttpSession session) {
		Risk  toUpdate = riskService.queryById(id);
		if (toUpdate == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
	if (StringUtils.isNoneBlank(kWord)) {
			toUpdate.setkWord(Strings.trimAndNotNull(kWord));
		}
	if (StringUtils.isNoneBlank(content)) {
		toUpdate.setContent(Strings.trimAndNotNull(content));
	}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	

	/**
	 * 删除
	 */
	
	@ResponseBody
	@RequestMapping(value = { "remove" })
	public String remove(int id) {
		Risk  risk = riskService.queryById(id);
		if (risk == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(risk);
		return CommonResp.buildSuccessResp().toJsonString();
	}
		
	/**
	 * 条件查询
	 */

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String kWord, HttpSession session) throws ParseException {
		
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(kWord)) {
			criterions.add(Restrictions.ilike("kWord", "%" + kWord + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("riskId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	
	/**
	 * 条件查询该用户的
	 */
	
	@ResponseBody
	@RequestMapping(value = "searchUser")
	public String searchUser(String kWord, Integer userId) throws ParseException {
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("userId", userId));
		if (StringUtils.isNoneBlank(kWord)) {
			criterions.add(Restrictions.ilike("kWord", "%" + kWord + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("riskId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
}
